package cl.uchile.sti.authorization.dao;

import cl.uchile.sti.authorization.model.Permiso;
import cl.uchile.sti.authorization.model.Rol;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Julio Villane
 * Date: 11-12-12
 * Time: 09:39 AM
 */
public class RolDAO {

    private static EntityManager entityManager = cambiarPorSpring.getEntityManager();

    private RolDAO(){}

    public static Rol find(Long id){
        return entityManager.find(Rol.class, id);
    }

    public static Rol save(Rol rol) {
        if (rol.getId() == null) {
            entityManager.persist(rol);
            return rol;
        } else {
            return entityManager.merge(rol);
        }
    }

    public static void delete(Rol rol){
        entityManager.remove(rol);
    }
    
    public static List<Rol> getByPermiso(Permiso permiso){
        Query query = entityManager.createNamedQuery("Rol.getByPermiso");
        query.setParameter("permiso", permiso);
        return query.getResultList();
    }
}
